﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace DS.ADSystem
{
    public class ImageUploadResult
    {
        public const string UrlPath = "http://img.valeb.cn/pic/x1/p1/";
        public const string HandlerPath = "/Upload/UploadHandler.ashx";
        public string error { get; set; }
        public string name { get; set; }
        public int size { get; set; }
        public string thumbnail_url { get; set; }
        public string url { get; set; }
        public string delete_type { get; set; }
        public string delete_url { get; set; }
        public bool delete_with_credentials { get; set; }

        public ImageUploadResult(string fileName, int fileLength, string fullPath,string Error=null)
        {
            InitProperty(fileName, fileLength, fullPath, Error);
        }

        public ImageUploadResult(FileInfo fileInfo) 
        {
            InitProperty(fileInfo.Name, (int)fileInfo.Length, fileInfo.FullName); 
        }

        private void InitProperty(string fileName, int fileLength, string fullPath, string Error = null)
        {
            name = fileName;
            size = fileLength;
            url = UrlPath + fileName;
            thumbnail_url = url;
            delete_type = "DELETE";
            delete_url = HandlerPath + "?img=" + fileName;
            delete_with_credentials = false;
            error = Error; 
        }

        private bool IsImage(string ext)
        {
            return ext == ".gif" || ext == ".jpg" || ext == ".png";
        }

        private string EncodeFile(string fileName)
        {
            return Convert.ToBase64String(System.IO.File.ReadAllBytes(fileName));
        }

        static double ConvertBytesToMegabytes(long bytes)
        {
            return (bytes / 1024f) / 1024f;
        }
    }
}